"use strict";
/* Controllers */
app.controller("dataInsightController", ['$scope', '$location', '$rootScope', '$routeParams', 'dataInsightService', function ($scope, $location, $rootScope, $routeParams, dataInsightService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.d = {};
    vm.p.id = $routeParams.id; // 如果id!=0为更新

    vm.p.dt = getDay(0);
    vm.p.dt2 = getDay(0);

    vm.d.topicDefine = [
        {topic:'zp_n_n_demo_flow', dims:[{name:'name',type:'string'},{name:'age',type:'number'},{name:'cate',type:'string'}], indexs:['1','amount']},
        {topic:'zp_n_n_app_action', dims:[{name:'58client',type:'string'},{name:'platform',type:"string"},{name:'cate',type:'string'}], indexs:['1','amount']},
        {topic:'zp_n_n_wx_detail', dims:[{name:'imei',type:'string'},{name:'city',type:'string'},{name:'cate',type:'string'}], indexs:['1','amount']}
    ];
    var errorFn = function (result) {
        alert(result);
    };


    $scope.changeSearchTime = function() {
        var vm = $scope.vm;
        vm.p.dt = getDay(parseInt(vm.search.ts));
        vm.p.dt2 = getDay(0);
    };
    $scope.appendDim = function(opt) {
        var item;
        if(vm.p.dim.type=='number') {
            item = vm.p.dim.name +"="+vm.p.dimVal;
        }else {
            item = vm.p.dim.name + "='"+vm.p.dimVal + "'";
        }
        if(vm.p.filter && vm.p.filter.length >2) {
            vm.p.filter = vm.p.filter+ " "+opt+" " + item;
        } else {
            vm.p.filter = item;
        }
    };

    $scope.querySeries = function () {
        var vm = $scope.vm;
        var params = {
            id: vm.p.id
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                //vm.p = JSON.stringify(response.data);
                vm.p = response.data;
            } else {
                alert(response.msg);
            }
        };
        //dataInsightService.getSeries(params, procFn, errorFn);
    };

    $scope.getYeWuResult = function() {
        var params = {dt:vm.p.dt, dt2:vm.p.dt2,dimList:vm.p.dimList};
        var procFn = function(response) {
            var data = response.data;
            if(!data) return;

            var dapanCountResult = data.dapanCountResult;
            if(dapanCountResult){
                if(dapanCountResult[0]&&dapanCountResult[1]) {
                    var t = dapanCountResult[0];
                    dapanCountResult[0] = dapanCountResult[1];
                    dapanCountResult[1] = t;
                }
                vm.dapanCountResult = dapanCountResult;
            }
            var yeWuResult = data.yeWuResult;
            if(yeWuResult && yeWuResult.length>0){
                vm.yeWuResult = yeWuResult;
                //插入排序，把同类的分到一起
                vm.yeWuResult[0].orderId = quataOrderId(vm.yeWuResult[0].id);
                for(var i = 1;i<vm.yeWuResult.length;i++){
                    vm.yeWuResult[i].orderId = quataOrderId(vm.yeWuResult[i].id);
                    var j = i;
                    while (j>0&&vm.yeWuResult[j].orderId<vm.yeWuResult[j-1].orderId) {
                        var temp = vm.yeWuResult[j];
                        vm.yeWuResult[j] = vm.yeWuResult[j-1];
                        vm.yeWuResult[j-1] = temp;
                        j--;
                    }
                }
                setTimeout(function(){
                    for(var i=0;i<yeWuResult.length;i++){
                        var item = yeWuResult[i];
                        var series =  [{
                            name: quataZhuName(item.id),
                            type: 'column',
                            yAxis: 1,
                            data: item.countList,
                            tooltip: {
                                valueSuffix: ''
                            }
                        }];
                        if(item.amount2List&&item.amount2List.length>0){
                            series.push({
                                name: quataLine2Name(item.id),
                                type: 'spline',
                                data: item.amount2List,
                                tooltip: {
                                    valueSuffix: ''
                                }
                            })
                        }
                        var quataLineNameRes = quataLineName(item.id);
                        if(item.amountList&&item.amountList.length>0) {
                            series.push({
                                name: quataLineName(item.id),
                                type: 'spline',
                                data: item.amountList,
                                tooltip: {
                                    valueSuffix: ''
                                }
                            });
                        }else {
                            quataLineNameRes = '';//不显示图例
                        }
                        drawXY("#zl_" + quataOrderId(item.id), quataName(item.id) , item.dateList, quataZhuName(item.id),quataLineNameRes,quataLine2Name(item.id),series);
                    }
                },100);

            }
        };
        dataInsightService.getYeWuResult(params, procFn, errorFn);
    };
    if(vm.p.id!=0) {
        console.log(vm.p.id);
        $scope.querySeries();
    }
}]);


/* Services */
app.service('dataInsightService', ['ajaxService', function (ajaxService) {

    this.getSeries = function (dataMetrics, successFunction, errorFunction) {
        ajaxService.AjaxPost(dataMetrics, "/star/api/basic-service/v1/insight/getSeries.do", successFunction, errorFunction);
    };

}]);

